﻿//
// TestLibraryWrapperSample.cs
// This file is part of Microsoft.WinAny.Helper.Samples project
//
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan) 
// Copyright (c) 2013 Josip Habjan. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.WinAny.Interop;

namespace Microsoft.WinAny.Helper.Samples.DynamicNativeLibrarySamples
{
    // delegates with a same signature like native dll

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate double AddV2_Handler(double val1, double val2);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate double AddV3_Handler(double val1, double val2, double val3);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate double AddV4_Handler(double val1, double val2, double val3, double val4);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate double Subtract_Handler(double val1, double val2);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate double Multiply_Handler(double val1, double val2);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate double Divide_Handler(double val1, double val2);


    /// <summary>
    /// Wrapper for a TestLibrary
    /// </summary>
    public class TestLibraryWrapperSample
    {
        private DynamicNativeLibrary _dnl;

        public AddV2_Handler AddV2;
        public AddV3_Handler AddV3;
        public AddV4_Handler AddV4;
        public Subtract_Handler Subtract;
        public Multiply_Handler Multiply;
        public Divide_Handler Divide;

        public TestLibraryWrapperSample(string fileName)
        {
            _dnl = new DynamicNativeLibrary(fileName);
            this.Attach();
        }

        public TestLibraryWrapperSample(byte[] buffer)
        {
            _dnl = new DynamicNativeLibrary(buffer);
            this.Attach();
        }

        private void Attach()
        {
            AddV2 = _dnl.GetDelegateForFunction("AddV2", typeof(AddV2_Handler)) as AddV2_Handler;
            AddV3 = _dnl.GetDelegateForFunction("AddV3", typeof(AddV3_Handler)) as AddV3_Handler;
            AddV4 = _dnl.GetDelegateForFunction("AddV4", typeof(AddV4_Handler)) as AddV4_Handler;
            Subtract = _dnl.GetDelegateForFunction("Subtract", typeof(Subtract_Handler)) as Subtract_Handler;
            Multiply = _dnl.GetDelegateForFunction("Multiply", typeof(Multiply_Handler)) as Multiply_Handler;
            Divide = _dnl.GetDelegateForFunction("Divide", typeof(Divide_Handler)) as Divide_Handler;
        }
    }

}
